package com.santoker.baseservice;

import com.santoker.baseservice.util.JSONUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Data
@NoArgsConstructor
@Slf4j
public class ApiResponseBody<T> {

    @ApiModelProperty(value = "状态码")
    private String code;

    @ApiModelProperty(value = "状态信息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private T data;

    protected ApiResponseBody(String code, String msg, T data) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public static <T> ApiResponseBody create(String code, String error, T data) {
        return new ApiResponseBody(code, error, data);
    }

    public static <T> ApiResponseBody createErrorBody(ApiResponseCode error, T data) {
        return new ApiResponseBody(error.getCode(), error.getMsg(), data);
    }

    public static ApiResponseBody createErrorBody(String code, String error) {
        return new ApiResponseBody(code, error, null);
    }

    public static <T> ApiResponseBody createSuccessBody(T data) {
        return new ApiResponseBody(ApiResponseCode.SUCCESS.getCode(), ApiResponseCode.SUCCESS.getMsg(), data);
    }

    public static <T> ApiResponseBody createSuccessBody() {
        return new ApiResponseBody(ApiResponseCode.SUCCESS.getCode(), ApiResponseCode.SUCCESS.getMsg(), null);
    }

    public static <T> void printRequestParam(T data) {
        log.info(">>>>>>>>>>> Request param : {}", data);
    }

    public static <T> void printRequestParam(T data, T data2) {
        log.info(">>>>>>>>>>> Request param : {}, {}", data, data2);
    }

    @Override
    public String toString() {
        return JSONUtil.jsonEncode(this);
    }

}
